#define DR_REG_RTCIO_BASE            0x3ff48400
#define RTC_IO_TOUCH_PAD2_REG        (DR_REG_RTCIO_BASE + 0x9c)
#define RTC_IO_TOUCH_PAD2_MUX_SEL_M  (BIT(19))
#define RTC_GPIO_OUT_REG             (DR_REG_RTCIO_BASE + 0x0)
#define RTC_GPIO_ENABLE_REG          (DR_REG_RTCIO_BASE + 0xc)
#define RTC_GPIO_ENABLE_S            14
#define RTC_GPIO_OUT_DATA_S          14
#define RTCIO_GPIO2_CHANNEL          12

.set gpio, RTCIO_GPIO2_CHANNEL
.set token, 0xcafe

.text
magic: .long 0
state: .long 0

.global entry
entry:
  move r0, magic
  ld r1, r0, 0
  sub r1, r1, token
  jump after_init, eq

init:
  WRITE_RTC_REG(RTC_IO_TOUCH_PAD2_REG, RTC_IO_TOUCH_PAD2_MUX_SEL_M, 1, 1);
  WRITE_RTC_REG(RTC_GPIO_ENABLE_REG, RTC_GPIO_ENABLE_S + gpio, 1, 1)
  move r0, magic
  move r1, token
  st r1, r0, 0

after_init:
  move r1, state
  ld r0, r1, 0
  move r2, 1
  sub r0, r2, r0
  st r0, r1, 0
  jumpr on, 0, gt
  jump off

on:
  WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S + gpio, 1, 1)
  jump exit

off:
  WRITE_RTC_REG(RTC_GPIO_OUT_REG, RTC_GPIO_OUT_DATA_S + gpio, 1, 0)
  jump exit

exit:
  halt